/*
 * Justin Prentice
 * TCSS 305
 * Tetris gui - piece preview
 */

package view;

import java.awt.Color;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.util.Observable;
import java.util.Observer;

import javax.swing.JPanel;

import model.Piece;

/**
 * A panel containing a preview of the upcoming piece.
 * @author Justin Prentice
 * @version 1.0
 */
@SuppressWarnings("serial")
public class PreviewPanel extends JPanel implements Observer
{
  
  
  /**
   * the number of sections to divide the preview panel into.
   */
  private static final int BLOCK_DIVISIONS = 6;
  
  /**
   * A multiplier so users can have scores ending in 0.
   */
  private static final int SCORE_MULTIPLE = 10;
  
  /**
   * The number of points required to advance a level.
   */
  private static final int LEVEL_THRESHOLD = 200;
  
  /**
   * The piece being previewed. 
   */
  private Piece my_piece;
  
  /**
   * The player's score.
   */
  private int my_score;
  
  /**
   * A color interpreter to decide what color to paint the block.
   */
  private final ColorInterpreter my_interpreter;

  /**
   * Sets up a panel for previewing the next piece.
   * @param the_interpreter the color interpreter to be used.
   */
  public PreviewPanel(final ColorInterpreter the_interpreter)
  {
    super();
    my_interpreter = the_interpreter;
    //setPreferredSize(new Dimension(DIMENSIONS, DIMENSIONS));
    //setBackground(Color.DARK_GRAY);
  }
  
  /**
   * Updates the preview panel.
   * @param the_observed the observable object.
   * @param the_update the update class
   */
  @Override
  public void update(final Observable the_observed, final Object the_update)
  {
    my_piece = ((TetrisUpdate) the_update).getPreviewPiece();
    my_score = ((TetrisUpdate) the_update).getScore() * SCORE_MULTIPLE;
    repaint();
  }
  @Override
  public void paintComponent(final Graphics the_graphics) 
  {
    super.paintComponent(the_graphics);
    final Graphics2D g2d = (Graphics2D) the_graphics;
    
    final GradientPaint gp = new GradientPaint(0, 0, Color.black, 
                                               getWidth(), getHeight(), Color.gray);
    g2d.setPaint(gp);
    g2d.fillRect(0, 0, getWidth(), getHeight());
    
    if (my_piece != null)
    {  
      g2d.setColor(my_interpreter.interpretColor(my_piece.getBlock()));
      final int[][] coordinates = my_piece.getCoordinates();
      
      final int blocksize = Math.min(getWidth(), getHeight()) / BLOCK_DIVISIONS;
      for (int i = 0; i < coordinates.length; i++)
      {
        g2d.draw3DRect((coordinates[i][1] + 1) * blocksize, 
                       (coordinates[i][0] + 1) * blocksize, 
                       blocksize, blocksize, true);
      }
      g2d.setColor(Color.LIGHT_GRAY);
      g2d.drawString(new String("  Score: " + my_score), 0, (BLOCK_DIVISIONS + 1) * blocksize);
      g2d.drawString(new String("  Level: " + my_score / LEVEL_THRESHOLD), 
                                 0 , (BLOCK_DIVISIONS * 2) * blocksize);
    }
  }
}
